package live.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;

public class Verifystring {
	
	private static Verifystring instance=null;
	
	
	public static synchronized Verifystring getInstance() {
		if(instance==null){
			instance = new Verifystring();
		}
		return instance;
	}

	/**
	 * POST发送数据格式化
	 * @param params
	 * @return
	 */
	public String returnVerifystring(Map<String, String> params){
		String result="";
		for (Entry<String, String> entry : params.entrySet()) {
			result += entry.getKey() + "=" + entry.getValue() + "&";
		}
		result += "merchantKey="+PostParam.MERCHANTKEY;
		return result;
	}
	
	/**
	 * 签名生成规则
	 * @param params
	 * @return
	 */
	public String returnAirMd5Hex(Map<String, String> params) {
		String result = "";
		String sign_key = AirParam.AIR_KEY.toString();
		List keys = new ArrayList(params.keySet());
		Collections.sort(keys);
		for (int i = 0; i < keys.size(); i++) {
			String key = (String) keys.get(i);
			Object value = params.get(key);
			if (i == keys.size() - 1) {
				result += key + "=" + value;
			} else {
				result += key + "=" + value + "&";
			}
		}
		result += sign_key;
		return DigestUtils.md5Hex(result.trim());
	}
}
